home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / ici / ici.cpi / mkvar.c < prev    next >
C/C++ Source or Header  |  1994-10-27  |  2KB  |  71 lines

  1. #include "exec.h"
  2. #include "struct.h"
  3. #include <stdarg.h>
  4.  
  5. /*
  6.  * mkvar(scope, name, typespec, value)
  7.  *
  8.  * This function is a simple way to define variables (to the ICI level).
  9.  * It takes a scope structure, name for the variable, a type specification
  10.  * (see below) and the variable's initial value and creates a variable in
  11.  * the current extern scope.
  12.  *
  13.  * Type specification is similar to other internal functions (ici_func,
  14.  * typecheck etc..) and uses the following key-letters
  15.  *
  16.  * Type Letter    ICI type    C type (of value)
  17.  * ================================================
  18.  *    i    int        long
  19.  *    f    float        double
  20.  *    s    string        char *
  21.  *    u    file        FILE *
  22.  *    o    any        object_t *
  23.  */
  24. int
  25. mkvar(struct_t *scope, char *name, int type, ...)
  26. {
  27.     object_t    *o;
  28.     string_t    *n;
  29.     va_list    va;
  30.  
  31.     va_start(va, type);
  32.     switch (type)
  33.     {
  34.     case 'i':
  35.     o = objof(new_int(va_arg(va, long)));
  36.     break;
  37.     case 'f':
  38.     o = objof(new_float(va_arg(va, double)));
  39.     break;
  40.     case 's':
  41.     o = objof(new_cname(va_arg(va, char *)));
  42.     break;
  43.     case 'u':
  44.     if ((n = new_cname(name)) == NULL)
  45.         return 1;
  46.     o = objof(new_file((char *)va_arg(va, FILE *), &stdio_ftype, n));
  47.     loose(n);
  48.     break;
  49.     case 'o':
  50.     if ((o = va_arg(va, object_t *)) == NULL)
  51.         error = "NULL object pointer given to mkvar()";
  52.     break;
  53.     default:
  54.     error = "illegal type key-letter given to mkvar()";
  55.     return 1;
  56.     }
  57.     va_end(va);
  58.     if
  59.     (
  60.     o == NULL
  61.     ||
  62.     (n = new_cname(name)) == NULL
  63.     ||
  64.     assign(scope, n, o)
  65.     )
  66.     return 1;
  67.     loose(o);
  68.     loose(n);
  69.     return 0;
  70. }
  71.